#include "Tokenizer.hpp"

namespace utils {

Tokenizer::Tokenizer(const std::string &input, const std::string &delimiters) {
  mInput = input;
  mDelimiters = delimiters;
  mIterator = mInput.begin();
  FillList();
}

Tokenizer::Tokenizer(const Tokenizer &toCopy) {
  mInput = toCopy.mInput;
  mDelimiters = toCopy.mDelimiters;
  mToken = toCopy.mToken;
  mIterator = toCopy.mIterator;
}

Tokenizer::~Tokenizer() {

}

Tokenizer &Tokenizer::operator =(const Tokenizer &toCopy) {
  if (this != &toCopy) {
    mInput = toCopy.mInput;
    mDelimiters = toCopy.mDelimiters;
    mToken = toCopy.mToken;
    mIterator = toCopy.mIterator;
  }
  return *this;
}

std::string Tokenizer::GetNextToken() {
  mToken = "";

  if (mInput.size() > 0) {
    for (; mIterator != mInput.end() && IsADelimiter(mIterator); ++mIterator)
      ;

    for (; mIterator != mInput.end() && !IsADelimiter(mIterator); ++mIterator) {
      mToken += *mIterator;
    }
  }
  return mToken;
}

std::list<std::string>& Tokenizer::GetTokensList() {
  return mTokenList;
}

void Tokenizer::FillList() {
  std::string token;

  while ((token = GetNextToken()).empty() == false) {
    mTokenList.push_back(token);
  }
}

Tokenizer::size_type Tokenizer::size() const {
  return mTokenList.size();
}

Tokenizer::iterator Tokenizer::begin() {
  return mTokenList.begin();
}

Tokenizer::const_iterator Tokenizer::begin() const {
  return mTokenList.begin();
}

Tokenizer::reverse_iterator Tokenizer::rbegin() {
  return mTokenList.rbegin();
}

Tokenizer::const_reverse_iterator Tokenizer::rbegin() const {
  return mTokenList.rbegin();
}

Tokenizer::iterator Tokenizer::end() {
  return mTokenList.end();
}

Tokenizer::const_iterator Tokenizer::end() const {
  return mTokenList.end();
}

Tokenizer::reverse_iterator Tokenizer::rend() {
  return mTokenList.rend();
}

Tokenizer::const_reverse_iterator Tokenizer::rend() const {
  return mTokenList.rend();
}

bool Tokenizer::IsADelimiter(std::string::const_iterator &iterator) const {
  return (mDelimiters.find(*iterator) != std::string::npos);
}

} /* !namespace utils */
